package cn.yx.application.gateway.hook;

import cn.yx.application.gateway.subscriber.MdcSkyWalkingSubscriber;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Hooks;
import reactor.core.publisher.Operators;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * <p>使用Reactor的Hooks启用MdcSkyWalkingSubscriber订阅器</p>
 *
 * @author Wgssmart
 */
@Component
public class MdcHook {

    private static final String KEY = "logMdc";

    @PostConstruct
    public void setHook() {
        Hooks.onEachOperator(KEY,
                Operators.lift((scannable, coreSubscriber) -> new MdcSkyWalkingSubscriber<>(coreSubscriber)));
    }

    @PreDestroy
    public void resetHook() {
        Hooks.resetOnEachOperator(KEY);
    }

}